﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CFZS.IDAL;
using System.Data.SqlClient;
using System.Data;
using CFZS.FrameWork.Utility;

namespace CFZS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:BasePerson
    /// </summary>
    public partial class BasePerson : IBasePerson
    {
        public BasePerson()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BasePerson");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CFZS.Model.BasePerson model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BasePerson(");
            strSql.Append("GuidPerson,Name,Birthday,Sex,BaseOrgSysCode,BaseTitle,BasePosition,Phone,Mobile,Email,Descr,IsActive,LineNo,Createdat,CreatedBy,CreatedByName,UpdatedAt,UpdatedBy,UpdatedByName)");
            strSql.Append(" values (");
            strSql.Append("@GuidPerson,@Name,@Birthday,@Sex,@BaseOrgSysCode,@BaseTitle,@BasePosition,@Phone,@Mobile,@Email,@Descr,@IsActive,@LineNo,@Createdat,@CreatedBy,@CreatedByName,@UpdatedAt,@UpdatedBy,@UpdatedByName)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@GuidPerson", SqlDbType.VarChar,32),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Birthday", SqlDbType.Date,3),
					new SqlParameter("@Sex", SqlDbType.VarChar,10),
					new SqlParameter("@BaseOrgSysCode", SqlDbType.BigInt,8),
					new SqlParameter("@BaseTitle", SqlDbType.VarChar,300),
					new SqlParameter("@BasePosition", SqlDbType.VarChar,300),
					new SqlParameter("@Phone", SqlDbType.VarChar,300),
					new SqlParameter("@Mobile", SqlDbType.VarChar,300),
					new SqlParameter("@Email", SqlDbType.VarChar,300),
					new SqlParameter("@Descr", SqlDbType.NText),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@LineNo", SqlDbType.BigInt,8),
					new SqlParameter("@Createdat", SqlDbType.DateTime),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,32),
					new SqlParameter("@CreatedByName", SqlDbType.VarChar,50),
					new SqlParameter("@UpdatedAt", SqlDbType.DateTime),
					new SqlParameter("@UpdatedBy", SqlDbType.VarChar,32),
					new SqlParameter("@UpdatedByName", SqlDbType.VarChar,50)};
            parameters[0].Value = model.GuidPerson;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Birthday;
            parameters[3].Value = model.Sex;
            parameters[4].Value = model.BaseOrgSysCode;
            parameters[5].Value = model.BaseTitle;
            parameters[6].Value = model.BasePosition;
            parameters[7].Value = model.Phone;
            parameters[8].Value = model.Mobile;
            parameters[9].Value = model.Email;
            parameters[10].Value = model.Descr;
            parameters[11].Value = model.IsActive;
            parameters[12].Value = model.LineNo;
            parameters[13].Value = model.Createdat;
            parameters[14].Value = model.CreatedBy;
            parameters[15].Value = model.CreatedByName;
            parameters[16].Value = model.UpdatedAt;
            parameters[17].Value = model.UpdatedBy;
            parameters[18].Value = model.UpdatedByName;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CFZS.Model.BasePerson model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BasePerson set ");
            strSql.Append("GuidPerson=@GuidPerson,");
            strSql.Append("Name=@Name,");
            strSql.Append("Birthday=@Birthday,");
            strSql.Append("Sex=@Sex,");
            strSql.Append("BaseOrgSysCode=@BaseOrgSysCode,");
            strSql.Append("BaseTitle=@BaseTitle,");
            strSql.Append("BasePosition=@BasePosition,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Email=@Email,");
            strSql.Append("Descr=@Descr,");
            strSql.Append("IsActive=@IsActive,");
            strSql.Append("LineNo=@LineNo,");
            strSql.Append("Createdat=@Createdat,");
            strSql.Append("CreatedBy=@CreatedBy,");
            strSql.Append("CreatedByName=@CreatedByName,");
            strSql.Append("UpdatedAt=@UpdatedAt,");
            strSql.Append("UpdatedBy=@UpdatedBy,");
            strSql.Append("UpdatedByName=@UpdatedByName");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@GuidPerson", SqlDbType.VarChar,32),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Birthday", SqlDbType.Date,3),
					new SqlParameter("@Sex", SqlDbType.VarChar,10),
					new SqlParameter("@BaseOrgSysCode", SqlDbType.BigInt,8),
					new SqlParameter("@BaseTitle", SqlDbType.VarChar,300),
					new SqlParameter("@BasePosition", SqlDbType.VarChar,300),
					new SqlParameter("@Phone", SqlDbType.VarChar,300),
					new SqlParameter("@Mobile", SqlDbType.VarChar,300),
					new SqlParameter("@Email", SqlDbType.VarChar,300),
					new SqlParameter("@Descr", SqlDbType.NText),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@LineNo", SqlDbType.BigInt,8),
					new SqlParameter("@Createdat", SqlDbType.DateTime),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,32),
					new SqlParameter("@CreatedByName", SqlDbType.VarChar,50),
					new SqlParameter("@UpdatedAt", SqlDbType.DateTime),
					new SqlParameter("@UpdatedBy", SqlDbType.VarChar,32),
					new SqlParameter("@UpdatedByName", SqlDbType.VarChar,50),
					new SqlParameter("@Id", SqlDbType.BigInt,8)};
            parameters[0].Value = model.GuidPerson;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Birthday;
            parameters[3].Value = model.Sex;
            parameters[4].Value = model.BaseOrgSysCode;
            parameters[5].Value = model.BaseTitle;
            parameters[6].Value = model.BasePosition;
            parameters[7].Value = model.Phone;
            parameters[8].Value = model.Mobile;
            parameters[9].Value = model.Email;
            parameters[10].Value = model.Descr;
            parameters[11].Value = model.IsActive;
            parameters[12].Value = model.LineNo;
            parameters[13].Value = model.Createdat;
            parameters[14].Value = model.CreatedBy;
            parameters[15].Value = model.CreatedByName;
            parameters[16].Value = model.UpdatedAt;
            parameters[17].Value = model.UpdatedBy;
            parameters[18].Value = model.UpdatedByName;
            parameters[19].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BasePerson ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BasePerson ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CFZS.Model.BasePerson GetModel(long Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,GuidPerson,Name,Birthday,Sex,BaseOrgSysCode,BaseTitle,BasePosition,Phone,Mobile,Email,Descr,IsActive,LineNo,Createdat,CreatedBy,CreatedByName,UpdatedAt,UpdatedBy,UpdatedByName from BasePerson ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;

            CFZS.Model.BasePerson model = new CFZS.Model.BasePerson();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = long.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GuidPerson"] != null && ds.Tables[0].Rows[0]["GuidPerson"].ToString() != "")
                {
                    model.GuidPerson = ds.Tables[0].Rows[0]["GuidPerson"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Birthday"] != null && ds.Tables[0].Rows[0]["Birthday"].ToString() != "")
                {
                    model.Birthday = DateTime.Parse(ds.Tables[0].Rows[0]["Birthday"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sex"] != null && ds.Tables[0].Rows[0]["Sex"].ToString() != "")
                {
                    model.Sex = ds.Tables[0].Rows[0]["Sex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BaseOrgSysCode"] != null && ds.Tables[0].Rows[0]["BaseOrgSysCode"].ToString() != "")
                {
                    model.BaseOrgSysCode = long.Parse(ds.Tables[0].Rows[0]["BaseOrgSysCode"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BaseTitle"] != null && ds.Tables[0].Rows[0]["BaseTitle"].ToString() != "")
                {
                    model.BaseTitle = ds.Tables[0].Rows[0]["BaseTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BasePosition"] != null && ds.Tables[0].Rows[0]["BasePosition"].ToString() != "")
                {
                    model.BasePosition = ds.Tables[0].Rows[0]["BasePosition"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Phone"] != null && ds.Tables[0].Rows[0]["Phone"].ToString() != "")
                {
                    model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Email"] != null && ds.Tables[0].Rows[0]["Email"].ToString() != "")
                {
                    model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Descr"] != null && ds.Tables[0].Rows[0]["Descr"].ToString() != "")
                {
                    model.Descr = ds.Tables[0].Rows[0]["Descr"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsActive"] != null && ds.Tables[0].Rows[0]["IsActive"].ToString() != "")
                {
                    model.IsActive = int.Parse(ds.Tables[0].Rows[0]["IsActive"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LineNo"] != null && ds.Tables[0].Rows[0]["LineNo"].ToString() != "")
                {
                    model.LineNo = long.Parse(ds.Tables[0].Rows[0]["LineNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Createdat"] != null && ds.Tables[0].Rows[0]["Createdat"].ToString() != "")
                {
                    model.Createdat = DateTime.Parse(ds.Tables[0].Rows[0]["Createdat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatedBy"] != null && ds.Tables[0].Rows[0]["CreatedBy"].ToString() != "")
                {
                    model.CreatedBy = ds.Tables[0].Rows[0]["CreatedBy"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CreatedByName"] != null && ds.Tables[0].Rows[0]["CreatedByName"].ToString() != "")
                {
                    model.CreatedByName = ds.Tables[0].Rows[0]["CreatedByName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UpdatedAt"] != null && ds.Tables[0].Rows[0]["UpdatedAt"].ToString() != "")
                {
                    model.UpdatedAt = DateTime.Parse(ds.Tables[0].Rows[0]["UpdatedAt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdatedBy"] != null && ds.Tables[0].Rows[0]["UpdatedBy"].ToString() != "")
                {
                    model.UpdatedBy = ds.Tables[0].Rows[0]["UpdatedBy"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UpdatedByName"] != null && ds.Tables[0].Rows[0]["UpdatedByName"].ToString() != "")
                {
                    model.UpdatedByName = ds.Tables[0].Rows[0]["UpdatedByName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,GuidPerson,Name,Birthday,Sex,BaseOrgSysCode,BaseTitle,BasePosition,Phone,Mobile,Email,Descr,IsActive,LineNo,Createdat,CreatedBy,CreatedByName,UpdatedAt,UpdatedBy,UpdatedByName ");
            strSql.Append(" FROM BasePerson ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,GuidPerson,Name,Birthday,Sex,BaseOrgSysCode,BaseTitle,BasePosition,Phone,Mobile,Email,Descr,IsActive,LineNo,Createdat,CreatedBy,CreatedByName,UpdatedAt,UpdatedBy,UpdatedByName ");
            strSql.Append(" FROM BasePerson ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM BasePerson ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from BasePerson T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "BasePerson";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
